package com.gomefinance;
import java.util.List;
/**
 * Created by Administrator on 2018/1/2.
 */
public class ProtocolGenUtil {
    //bson
    private static StringBuffer bsonAMessage = new StringBuffer();
    private static StringBuffer bsonARequest = new StringBuffer();
    private static StringBuffer bsonAResponse = new StringBuffer();
    private static StringBuffer bsonAActorMessage = new StringBuffer();
    private static StringBuffer bsonAActorRequest = new StringBuffer();
    private static StringBuffer bsonAActorResponse = new StringBuffer();
    //protocolbuf
    private static StringBuffer protocolAMessage = new StringBuffer();
    private static StringBuffer protocolARequest = new StringBuffer();
    private static StringBuffer protocolAResponse = new StringBuffer();
    private static StringBuffer protocolAActorMessage = new StringBuffer();
    private static StringBuffer protocolAActorRequest = new StringBuffer();
    private static StringBuffer protocolAActorResponse = new StringBuffer();

    //opcode
    private static StringBuffer opcode = new StringBuffer();
    //class
    private static StringBuffer clazz = new StringBuffer();

    public static void genProtocol( List<String> rowlist) throws Exception {
        if(rowlist.size()==0){
            return  ;
        }
        if(rowlist.get(0).equals("协议名称")){
            return ;
        }
        //opcode
        opcode.append(rowlist.get(1)+",\n");
        //statement
        genStatement(rowlist);
        //class
        genClass(rowlist);
    }



    public static void endRead(){
        System.out.println("opcode:...................");
        System.out.println(opcode);

        System.out.println("AMessage........................");
        System.out.println(bsonAMessage);
        System.out.println(protocolAMessage);
        System.out.println("ARequest................");
        System.out.println(bsonARequest);
        System.out.println(protocolARequest);
        System.out.println("AResponse......................");
        System.out.println(bsonAResponse);
        System.out.println(protocolAResponse);

        System.out.println("AActorMessage........................");
        System.out.println(bsonAActorMessage);
        System.out.println(protocolAActorMessage);

        System.out.println("AActorRequest............................");
        System.out.println(bsonAActorRequest);
        System.out.println(protocolAActorResponse);
        System.out.println("AActorResponse.......................");
        System.out.println(bsonAActorResponse);
        System.out.println(protocolAActorRequest);
        System.out.println("class................................");
        System.out.println(clazz);

    }


    private static void genStatement(List<String> rowlist){

        String s = rowlist.get(1);
        //普通请求
        if(s.contains("Req") && !s.contains("Actor")){
            bsonARequest.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolARequest .append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        }
        //普通响应
        else if(s.contains("Res") && !s.contains("Actor")){
            bsonAResponse.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolAResponse .append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        }
        //actor请求
        else if(s.contains("Req")&& s.contains("Actor")){
            bsonAActorRequest.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolAActorRequest .append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        }
        //actor响应
        else if(s.contains("Res")&& s.contains("Actor")){
            bsonAActorResponse.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolAActorResponse .append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        }
        //普通消息
        else if(!s.contains("Req")&& !s.contains("Actor") &&!s.contains("Res") ){
            bsonAMessage.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolAMessage.append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        //普通Actor
        }else if(!s.contains("Res")&& s.contains("Actor")&&!s.contains("Req")){
            bsonAActorMessage.append("[BsonKnownTypes(typeof("+s+"))]\n" );
            protocolAActorMessage .append("[ProtoInclude((int)Opcode."+s+", typeof("+s+"))]\n");
        }
    }

    //生成class
    public static void genClass(List<String > rowList) {
        StringBuffer sb = new StringBuffer();
        sb.append("/// <summary>\n" +
                "      /// "+rowList.get(0)+"\n" +
                "     /// </summary>\n" +
                "    [ProtoContract]\n"+
                "    [Message(Opcode."+rowList.get(1)+")]\n"+
                "    public class "+rowList.get(1)+" : "+getParent(rowList.get(1))+" {\n" +
                genClassFields(rowList.get(3))+
                "    }\n");
        clazz.append(sb);
    }

    private static String genClassFields(String fields) {
        String res = "";
        String[] fieldArr = fields.split(",");
        if(fieldArr.length>0){
            for (int i = 0 ;i<fieldArr.length;i++){
                 if(StringUtils.isEmpty(fieldArr[i])|| fieldArr[i].trim().equals("")){
                     continue;
                 }
                String[] fieldTypeAndName = getFieldTyepAndName(fieldArr[i]);
                res+="[ProtoMember("+i+")]\n";
                try{
                    res += "public " + fieldTypeAndName[0] + " " + fieldTypeAndName[1] + ";\n";
                }catch (Exception e){
                   // System.out.println(fieldArr[i]+"------------------------------------------------------------------------------");
                    res="";
                }
            }
        }

        return res+"\n";
    }

    private static String[] getFieldTyepAndName(String s) {
        return s.split("_");
    }

    private static String getParent(String s) {
        //普通请求
        if(s.contains("Req") && !s.contains("Actor")){
            return "ARequest";
        }
        //普通响应
        else if(s.contains("Res") && !s.contains("Actor")){
            return "AResponse";
        }
        //actor请求
        else if(s.contains("Req")&& s.contains("Actor")){
            return "AActorRequest";
        }
        //actor响应
        else if(s.contains("Res")&& s.contains("Actor")){
            return "AActorResponse";
        }
        //普通消息
        else if(!s.contains("Req")&& !s.contains("Actor") &&!s.contains("Res") ){
            return "AMessage";
            //普通Actor
        }else if(!s.contains("Res")&& s.contains("Actor")&&!s.contains("Req")){
            return "AActorMessage";
        }
        return null;
    }


}
